package com.io.netty.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class TestServer {
    public static void main(String[] args) throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            //初始化serverBootstrap
            serverBootstrap.group(bossGroup, workerGroup)
                    //服务器端使用的是NioServerSocketChannel.class，客户端使用的是NioSocketChannel.class
                    .channel(NioServerSocketChannel.class).
                    //childHandler中要传入一个初始化器，然后在初始化器中获取ChannelPipeline对象
                    childHandler(new TestServerInitializer());

            //由bossGroup同步监听的，当前线程不会阻塞在这（判断会不会阻塞很简单，打印一条输出语句就行了）
            ChannelFuture channelFuture = serverBootstrap.bind(8979).sync();

            //当前线程会阻塞在这，通道关闭时才会继续往下执行（判断会不会阻塞很简单，打印一条输出语句就行了）
            channelFuture.channel().closeFuture().sync();

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
